package com.aopcinemas.common.model;

import jade.content.Concept;

//TODO: Ilja See algorithms for GPS system and coordinates
public class Location implements Concept {

    private static final long serialVersionUID = 1L;

    private double latitude;
    private double longitude;

    private static final double RADIO = 6378.16;

    public Location(double latitude, double longitude) {
        this.latitude = latitude;
        this.longitude = longitude;
    }

    public double getLatitude() {
        return latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    public static double toRadians(double x) {
        return x * Math.PI / 180;
    }

    public double distance(Location loc) {
        return distance(this, loc);
    }

    // ALGORITHMS:
    // http://stackoverflow.com/questions/27928/how-do-i-calculate-distance-between-two-latitude-longitude-points
    // http://www.movable-type.co.uk/scripts/latlong.html
    public static double distance(Location loc1, Location loc2) {
        double dlon = toRadians(loc2.longitude - loc1.longitude);
        double dlat = toRadians(loc2.latitude - loc2.latitude);

        double a = (Math.sin(dlat / 2) * Math.sin(dlat / 2)) + Math.cos(toRadians(loc1.latitude)) * Math.cos(toRadians(loc2.latitude)) * (Math.sin(dlon / 2) * Math.sin(dlon / 2));
        double angle = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        return angle * RADIO;
    }

}
